<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {
	
	var $PW = '';
	
	function __construct()
	{
		parent::__construct();
	}

	function index()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="eError">', '</div>');
		$this->form_validation->set_rules('user', '用户名', 'trim|required|min_length[3]|max_length[200]|xss_clean|callback_user_check');
		$this->form_validation->set_rules('pass', '密码', 'trim|required|min_length[3]|max_length[200]|xss_clean|callback_pass_check');
		
		if ($this->form_validation->run() == TRUE)
		{
			$eMemo = (int)$this->input->post('memo');
			$eUser = $this->input->post('user');
			
			$this->input->set_cookie('eu', $eUser, 31536000);
			
			$eData = array('loginname'  => $eUser,
						   'logged_in' => TRUE);
			 
			$this->session->set_userdata($eData);
			
			redirect('mang/manage');
		}
		else
		  $this->load->view('mang/login', array('eu' => $this->input->cookie('eu')));
	}
	
	function user_check($str)
	{
		$this->db->where('u_name like ', $str);
		$eRow = $this->db->get('users')->row();
		if( ! $eRow)
		{
			$this->form_validation->set_message('user_check', 'Enter your Users address.');
			return FALSE;
		}
		else
		{
			$this->PW = $eRow->u_password;
			return TRUE;
		}
	}
	
	function pass_check($str)
	{
		if($this->PW != md5(md5($str).'ecomma'))
		{
			$this->form_validation->set_message('pass_check', 'Enter your Password address.');
			return FALSE;
		}
		else return TRUE;
	}
	
	function logout()
	{
		$this->session->unset_userdata('username');
		$this->session->unset_userdata('logged_in');
		
		redirect('mang/welcome');
	}
}